import {createEmptyUserTask} from '@/components/flow/typs/flow.js';
import FlowModelInstance from '@/components/flow/typs/FlowModelInstance.js';
import {openModal} from '@/utils/index.js';

export default class AddNodeCommand {
  userTask;
  supportInstance;
  nodeId;

  constructor(supportInstance, nodeId) {
    this.supportInstance = supportInstance;
    this.nodeId = nodeId;
  }

  serve() {
    this.userTask = createEmptyUserTask();
    this.editUserTask();
  }

  editUserTask() {
    let userTask = this.userTask;
    openModal(this.supportInstance.userTaskConfig, {userTask, model: this.supportInstance.model, globalProperties: this.supportInstance.globalProperties, supportInstance: this.supportInstance}, (userTask) => this.ok(userTask));
  }

  ok(userTask) {
    userTask = userTask || this.userTask;
    let list = userTask.multiUserTask && userTask.multiUserTask.length && userTask.multiUserTask || [userTask];
    let flowModelInstance = new FlowModelInstance(this.supportInstance.getModel());
    list.forEach(t => flowModelInstance.addNodeAfter(t, this.nodeId));
    this.supportInstance.setModel(flowModelInstance.getModelString());
  }
}